package com.sz.jinzheng.controller;

import com.sz.jinzheng.model.BillForm;
import com.sz.jinzheng.model.Result;
import com.sz.jinzheng.service.BorrowInfoService;
import com.sz.jinzheng.service.BorrowerInfoService;
import com.sz.jinzheng.service.TbillFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;

@Slf4j
@RequestMapping("/user")
@RestController
public class ReturnController {
    @Autowired
    private TbillFormService tbillFormService;
    @Resource
    private BorrowInfoService borrowInfoService;

    @RequestMapping("/Prepayment")
    private Result Prepayment(@RequestBody BillForm billForm1) {
        Integer borrowInfoId = billForm1.getBorrowInfoId();
        /*下面的借款单borrowinfoid写死了 记得改一下*/
        log.info("这是222的=");
        Double PrepaymentAmount = 0.00;//这是未还的金额
        List<BillForm> billForms = tbillFormService.getArrears(borrowInfoId);
        System.out.println("billForms = " + billForms);
       /* int u = 0;//初始化一个数下面用来计数 看还了几次
        Iterator<BillForm> iterator = billForms.iterator();
        for (BillForm billForm : billForms) {
            Double capital = iterator.next().getPrincipalAndInterest();
            u++;
            log.info("===" + capital);
            log.info("还了几期=" + u);
        }*/
        if (billForms.size() >= 3) {//已还款三个月及以上
            log.info("55555");
            List<BillForm> arrears = tbillFormService.getArrears1(borrowInfoId);//差未还金额
            System.out.println("arrears = " + arrears);
            log.info("55555");
            Double sum = 0.00;//未还的总额Iterator<BillForm> iterator = billForms.iterator();
            //  Iterator<BillForm> iterator2 = billForms.iterator();
            for (BillForm arrear : arrears) {
                log.info("55555");

                // Double capital = iterator2.next().getCapital();
                //  System.out.println("capital = " + capital);
                sum = sum + arrear.getPrincipalAndInterest();
                log.info("55555");


                log.info("未还金额=" + sum);
            }
            //提前还款金额
            PrepaymentAmount = sum * (1 + 0.05);
            System.out.println("PrepaymentAmount = " + PrepaymentAmount);
            DecimalFormat df = new DecimalFormat("#.00");
            String format = df.format(PrepaymentAmount);
            return new Result(200, "提前还款需还金额", format);

        }
        log.info("88888");
        return new Result(101, "提前还款失败", "需要还款成功三个月以上才可以提前还款哦！");


    }

    /**
     * 修改一个月的还款支付
     *
     * @param billForm1
     * @return
     */
    @RequestMapping("/updateForm")//直接点单还款过后就修改账
    public Result updateForm(@RequestBody BillForm billForm1) {//还款过后修改账单
        System.out.println("billForm1 = " + billForm1.getBorrowInfoId());
        System.out.println("billForm1 = " + billForm1.getWhichIssue());
        log.info("====huan款");
        int i = tbillFormService.updateForm(billForm1);//修改账单为已还款
        return new Result(200, "还款成功", i);//返回修改几行
    }

    /**
     * 用户提前还款成功！
     *
     * @param billForm1
     * @return
     */
    @PostMapping("/payPrepayment")
    public Result payPrepayment(@RequestBody BillForm billForm1) {
        int i = tbillFormService.payPrepayment(billForm1.getBorrowInfoId());
        if (i > 0) {
            //大于零即还款成功，同时修改总借款信息的状态
            borrowInfoService.updateRepaymentStatus(billForm1.getBorrowInfoId());
            return new Result(200, "还款成功", i);//返回修改几行
        }
        return new Result(444, "还款失败", "系统内部错误！");//返回修改几行
    }

}
